﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using System.Resources;
using System.Reflection;
namespace QLTV
{
    public class BaseForm : DevExpress.XtraEditors.XtraForm
    {
        public DataSet Datasource { get; set; }
        public string UserName { get; set; }
        public string cItemKey { get; set; }
        public Permissions MyPermission { get; set; }
        public ResourceManager rm;
        public System.Globalization.CultureInfo myCultureInfo;
      
        #region show waiting message
        protected Forms.frmPleaseWait _wait;
        protected void RunLongThread(ThreadStart loaddatta)
        {
            lock (this)
            {
                Thread lengthyProcessThread = new Thread(loaddatta) { IsBackground = true };
                lengthyProcessThread.Start();
                _wait = new Forms.frmPleaseWait();
                _wait.ShowDialog();
            }
        }
        #endregion

        public BaseForm()
        {
            if (!String.IsNullOrEmpty(Program.language))
            {
                myCultureInfo = new CultureInfo(Program.language);
                Thread.CurrentThread.CurrentUICulture = myCultureInfo;
            }
            rm = new ResourceManager("QLTV.Properties.Resources", Assembly.GetExecutingAssembly());
        }
        protected override void OnLoad(EventArgs e)
        {
            if (Datasource != null)
            {
                using (QLTV.Database.dsPermissionTableAdapters.tblQuyenTableAdapter tadQuyen = new Database.dsPermissionTableAdapters.tblQuyenTableAdapter())
                {
                    cItemKey = this.Name;
                    UserName = Program.username;
                    //use databinding to set permission
                    tadQuyen.LayQuyen((QLTV.Database.dsPermission.tblQuyenDataTable)(Datasource.Tables[0]), UserName, cItemKey);
                    //or use by code
                    Permissions query = (from per in Datasource.Tables[0].AsEnumerable()
                                         select new Permissions
                                         {
                                             isAdmin = per.Field<Boolean>("isAdmin"),
                                             username = per.Field<string>("cUserName"),
                                             formKey = per.Field<string>("cItemKey"),
                                             Full = per.Field<Boolean>("bFull"),
                                             TruyCap = per.Field<Boolean>("bTruyCap"),
                                             Them = per.Field<Boolean>("bThem"),
                                             Sua = per.Field<Boolean>("bSua"),
                                             Xoa = per.Field<Boolean>("bXoa"),
                                             In = per.Field<Boolean>("bIn"),
                                             Nhap = per.Field<Boolean>("bNhap"),
                                             Xuat = per.Field<Boolean>("bXuat")

                                         }).SingleOrDefault<Permissions>();
                    this.MyPermission = query;
                }
            }
            base.OnLoad(e);
        }
        private void focusApperance()
        {

        }
    }
    public class RibbonBase : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public DataSet Datasource { get; set; }
        public string UserName { get; set; }
        public string cItemKey { get; set; }
        public Permissions MyPermissionPerForm { get; set; }
        public List<Permissions> MyFullPermission { get; set; }
        public ResourceManager rm;
        public System.Globalization.CultureInfo myCultureInfo;
        public RibbonBase()
        {
            if (!String.IsNullOrEmpty(Program.language))
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(Program.language);
            rm = new ResourceManager("QLTV.Properties.Resources", Assembly.GetExecutingAssembly());
        }


        protected override void OnLoad(EventArgs e)
        {
            if (Datasource != null)
            {
                using (QLTV.Database.dsPermissionTableAdapters.tblQuyenTableAdapter tadQuyen = new Database.dsPermissionTableAdapters.tblQuyenTableAdapter())
                {
                    cItemKey = this.Name;
                    UserName = Program.username;
                    //use databinding to set permission
                    tadQuyen.LayQuyen((QLTV.Database.dsPermission.tblQuyenDataTable)(Datasource.Tables[0]), UserName, cItemKey);
                    //or use by code
                    Permissions permission = (from per in Datasource.Tables[0].AsEnumerable()
                                              select new Permissions
                                              {
                                                  isAdmin = per.Field<Boolean>("isAdmin"),
                                                  username = per.Field<string>("cUserName"),
                                                  formKey = per.Field<string>("cItemKey"),
                                                  Full = per.Field<Boolean>("bFull"),
                                                  TruyCap = per.Field<Boolean>("bTruyCap"),
                                                  Them = per.Field<Boolean>("bThem"),
                                                  Sua = per.Field<Boolean>("bSua"),
                                                  Xoa = per.Field<Boolean>("bXoa"),
                                                  In = per.Field<Boolean>("bIn"),
                                                  Nhap = per.Field<Boolean>("bNhap"),
                                                  Xuat = per.Field<Boolean>("bXuat")

                                              }).SingleOrDefault<Permissions>();
                    this.MyPermissionPerForm = permission;
                    // permission.Where<Permissions>();

                }
            }
            base.OnLoad(e);
        }
    }
    public class Permissions
    {
        public string username { get; set; }
        public Boolean isAdmin { get; set; }
        public string formKey { get; set; }
        public Boolean Full { get; set; }
        public Boolean TruyCap { get; set; }
        public Boolean Them { get; set; }
        public Boolean Sua { get; set; }
        public Boolean Xoa { get; set; }
        public Boolean In { get; set; }
        public Boolean Nhap { get; set; }
        public Boolean Xuat { get; set; }

    }
}
